﻿// Author: Hristo Hristov
// Date: 14.08.11
// Revision 8

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace NOVA.Graphics
{
    /// <summary>
    /// Material class representing the graphics material. Supports solid, normal and parallax texture mapping.
    /// </summary>
    public class RenderMaterial
    {
        #region Members

        bool m_bTextureSet;
        bool m_bNormalMapSet;
        bool m_bHeightMapSet;
        bool m_bSpecularMapSet;

        Vector4 m_diffuseColor;
        Vector4 m_specularColor;

        Texture2D m_texture;
        Texture2D m_normalMap;
        Texture2D m_specularMap;
        Texture2D m_heightMap;

        #endregion

        #region Properties

        /// <summary>
        /// Set if a texture was defined.
        /// </summary>
        public bool TextureSet
        {
            get { return m_bTextureSet; }
        }

        /// <summary>
        /// Set if a normal map was defined.
        /// </summary>
        public bool NormalMapSet
        {
            get { return m_bNormalMapSet; }
        }

        /// <summary>
        /// Set if a height map was defined.
        /// </summary>
        public bool HeightMapSet
        {
            get { return m_bHeightMapSet; }
        }

        /// <summary>
        /// Gets or sets the diffuse color.
        /// </summary>
        public Vector4 Diffuse 
        {
            get { return m_diffuseColor; }
            set
            {
                m_diffuseColor = value;
                
                // If no texture was specified, create a texture for the deferred rendering depending on the diffuse color
                //if (!m_bTextureSet)
                //{
                //    m_texture = new Texture2D(Core.Device, 512, 512, false, SurfaceFormat.Color);

                //    Color[] data = new Color[512*512];

                //    Color color = new Color((int)(value.X*255f), (int)(value.Y*255f), (int)(value.Z*255f), (int)(value.W*255f));

                //    for (int i = 0; i < 512*512; i++)
                //        data[i] = color;

                //    m_texture.SetData<Color>(data);
                //}
            }
        }

        /// <summary>
        /// Gets or sets the specular color.
        /// </summary>
        public Vector4 Specular 
        {
            get { return m_specularColor; }
            set
            {
                m_specularColor = value;

                //// If no texture was specified, create a texture for the deferred rendering depending on the diffuse color
                //if (!m_bSpecularMapSet)
                //{
                //    m_specularMap = new Texture2D(Core.Device, 512, 512, false, SurfaceFormat.Color);

                //    Color[] data = new Color[512 * 512];

                //    Color color = new Color((int)(value.X * 255f), (int)(value.Y * 255f), (int)(value.Z * 255f), (int)(value.W * 255f));

                //    for (int i = 0; i < 512 * 512; i++)
                //        data[i] = color;

                //    m_specularMap.SetData<Color>(data);
                //}
            }
        }

        /// <summary>
        /// Gets or sets the ambient color.
        /// </summary>
        public Vector4 Ambient { get; set; }

        /// <summary>
        /// Gets or sets the emissive color.
        /// </summary>
        public Vector4 Emissive { get; set; }

        /// <summary>
        /// Gets or sets the specular power.
        /// </summary>
        public float SpecularPower { get; set; }

        /// <summary>
        /// Gets or sets the texture.
        /// </summary>
        public Texture2D Texture 
        {
            get { return m_texture; }
            set 
            { 
                m_texture = value; 
                m_bTextureSet = true;
            }
        }

        /// <summary>
        /// Gets or sets the normal map.
        /// </summary>
        public Texture2D NormalMap 
        {
            get { return m_normalMap; }
            set 
            { 
                m_normalMap = value;
                m_bNormalMapSet = true;
            }
        }

        /// <summary>
        /// Gets or sets the height map.
        /// </summary>
        public Texture2D HeightMap 
        {
            get { return m_heightMap; }
            set
            {
                m_heightMap = value;
                m_bHeightMapSet = true;
            }
        }

        /// <summary>
        /// Gets or sets the specular map.
        /// </summary>
        public Texture2D SpecularMap
        {
            get { return m_specularMap; }
            set { m_specularMap = value; m_bSpecularMapSet = true; }
        }

        /// <summary>
        /// Gets or sets the transparency level between zero and one. The default is zero.
        /// </summary>
        public float Transparency { get; set; }

        /// <summary>
        /// Gets or sets the glowing color. The default is gold.
        /// </summary>
        public Vector3 GlowColor { get; set; }

        /// <summary>
        /// Gets or sets the glowing factor. Setting this to zero, creates a uniform glowing. The default value is 0.8.
        /// </summary>
        public float GlowFactor { get; set; }

        /// <summary>
        /// If enabled, the objects has glowing effect using the glow color and factor.
        /// </summary>
        public bool EnableGlow { get; set; }
        #endregion

        #region Constructor
        public RenderMaterial()
        {
            m_bTextureSet = false;
            m_bSpecularMapSet = false;

            Diffuse = Vector4.Zero;
            Specular = Color.White.ToVector4();
            
            Ambient = Vector4.Zero;
            Emissive = Vector4.Zero;

            m_normalMap = Core.EmbeddedContent.Load<Texture2D>("RandomNormal");//State.Content.Load<Texture2D>("Textures\\RandomNormal");

            SpecularPower = 10.0f;

            Transparency = 0.0f;

            GlowColor = Color.Gold.ToVector3();
            GlowFactor = 0.8f;
            EnableGlow = false;
        }
        #endregion

    }
}
